<?php


namespace app\common\dao\activity;

use app\common\model\activity\NewActivity;
use app\common\repositories\store\product\SpuRepository;
use think\Collection;
use think\db\BaseQuery;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use app\common\dao\BaseDao;
use app\common\model\article\Article;
use app\common\model\BaseModel;
use think\Model;

class NewActivityDao extends BaseDao
{

    /**
     * @return string
     * @author: fupeng
     * @day: 2024/1/28
     */
    protected function getModel(): string
    {
        return NewActivity::class;
    }

    /**
     * @return Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2024/1/28
     */
    public function getAll()
    {
        return NewActivity::getDB()->select();
    }

    /**
     * @param array $where
     * @return BaseQuery
     * @author: fupeng
     * @day: 2024/1/28
     */
    public function search(array $where)
    {
        $query = NewActivity::getDB();
        if (isset($where['title']) && $where['title'] !== '') $query->whereLike('title', "%{$where['title']}%");
        if (isset($where['status']) && $where['status'] !== '') $query->where('status', $where['status']);
        if (isset($where['not_id']) && $where['not_id'] > 0) $query->where('new_activity_id', '<>',$where['not_id']);
        $query->when(isset($where['type']) && $where['type'] !== '',function($query)use($where){
        switch ($where['type']){
            case 0: //未开始
                $query->where('status',1);
                $query->whereTime('start_time','>',time());
                break;
            case 1: //进行中
                $query->where('status',1)
                    ->whereTime('start_time','<=',time())
                    ->whereTime('end_time','>',time());
                break;
            case 2: //已结束
                $query->whereOr('end_time','<= TIME',time());
                break;
            case 3: //未结束的活动
                $query->where('end_time','>= TIME',time())->where('status',1);
                break;
            default: //已关闭
                $query->where('status',0);
                break;
        }
    });
        $query->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
            getModelTime($query, $where['date'], 'create_time');
        });
        return $query->where('is_del',0);
    }


    public function switchStatus($id, $data)
    {
        return ($this->getModel())::getDB()->where($this->getPk(),$id)->update($data);
    }

    public function valActiveStatus()
    {
        $time = date('Y-m-d H:i:s',time());
        $id = $this->getModel()::getDB()->where('state',1)
            ->whereTime('end_time','<=',$time)
            ->column('new_activity_id');
        if($id) {
            $this->getModel()::getDB()->where('new_activity_id', 'in', $id)->update(['state' => -1]);
        }

        $id = $this->getModel()::getDB()
            ->where('status',1)
            ->where('state',0)
            ->where('is_del',0)
            ->whereTime('start_time','>=',$time)
            ->whereTime('end_time','<=',$time)
            ->column('new_activity_id');
        if($id) {
            $this->getModel()::getDB()->where('new_activity_id', 'in', $id)->update(['state' => 1]);
        }
    }


}
